<?php
require_once (dirname(__FILE__).'/../../prepend.php');
global $bdd;
global $logger;

/**
 * Example construction of a SEPA direct debit (SDD) message in XML format.
 *
 * @author     Johannes Feichtner <johannes@web-wack.at>
 * @copyright  http://www.web-wack.at web wack creations
 * @license    http://creativecommons.org/licenses/by-nc/3.0/ CC Attribution-NonCommercial 3.0 license
 * For commercial use please contact sales@web-wack.at
 */

require_once('class.SEPADirectDebitTransaction.php');
require_once('class.SEPAException.php');
require_once('class.SEPAGroupHeader.php');
require_once('class.SEPAMessage.php');
require_once('class.SEPAPaymentInfo.php');
require_once('class.URLify.php');

$message = new SEPAMessage('urn:iso:std:iso:20022:tech:xsd:pain.008.003.02');
$groupHeader = new SEPAGroupHeader(); // (1..1)
$groupHeader->setMessageIdentification('SEPA-'.time()); // ID Unique
$groupHeader->setInitiatingPartyName('HTO'); // Nom du crediteur
$message->setGroupHeader($groupHeader);

$paymentInfo = new SEPAPaymentInfo(); // (1..n)
$paymentInfo->setPaymentInformationIdentification('SEPA-'.time()); // ID Unique 
$paymentInfo->setBatchBooking('false');
$paymentInfo->setLocalInstrumentCode('CORE'); // Autres options: COR1, B2B
// Type et date
$paymentInfo->setSequenceType('RCUR');
// CORE: FRST: +6 days, RCUR: +3 days, FNAL: +3 days, OOFF: +6 days
// B2B: All +2 days
$paymentInfo->setRequestedCollectionDate(date('Y-m-d', strtotime('+1 days')));
// Account on which payment should be recieved
$paymentInfo->setCreditorName('HTO'); // Nom du crediteur
$paymentInfo->setCreditorAccountIBAN('DE89370400440532013000');  					//////////////////////////////////////  A MODIFIER
$paymentInfo->setCreditorAgentBIC('PBNKDEFF760');									//////////////////////////////////////  A MODIFIER
$paymentInfo->setCreditorSchemeIdentification('DE22ZZZ00000012345');				//////////////////////////////////////  A MODIFIER

//liste les factures 
$requete = $bdd->prepare('SELECT * FROM admin_facture WHERE facture NOT LIKE "%temp%" ORDER BY id_client ');
$requete->execute();
$results=$requete->fetchall(PDO::FETCH_ASSOC);
//var_dump($results);
$x=1;

foreach($results as $key => $facture)
{	
	//recuperation des éléments du client
	$requete = $bdd->prepare('SELECT * FROM fiche_client WHERE raison_social = "'.$results[$key]['nom'].'" ');
	$requete->execute();
	$client=$requete->fetchall(PDO::FETCH_ASSOC);
	
	//mise en forme du montant
	$montant_facture = number_format($results[$key]['total_facturation']*1.2,2,'.',' ');// calcul de la tva: 20%

	// création de l'iban en retirant les espaces
	$iban = trim($client[0]['code_iban']).trim($client[0]['code_etablissement']).trim($client[0]['code_guichet']).trim($client[0]['num_compte']).trim($client[0]['rib']);

$transaction = new SEPADirectDebitTransaction(); // (1..n)
$transaction->setEndToEndIdentification($client[0]['code_client_hto']); // Vu par le débiteur
$transaction->setInstructedAmount($montant_facture);
$transaction->setMandateIdentification('NOM'); // Vu par le débiteur
$transaction->setDateOfSignature(date("Y-m-d"));
//$transaction->setAmendmentIndicator('false');
$transaction->setDebtorName($client[0]['raison_social']);
$transaction->setDebtorIban($iban);
$transaction->setDebtorAgentBIC($client[0]['swift']);
$transaction->setRemittanceInformation("Facture ".date("d/m/Y")); // Vu par le débiteur
$paymentInfo->addTransaction($transaction);


$x++;
}
$message->addPaymentInfo($paymentInfo);
//if ($message->validateXML('inc/SEPA2/validation_schemes/pain.008.003.02.xsd'))
	
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename='.date("Ymd").'-SEPA.xml');

echo $message->printXML();
?>
<?php
//redirect("../../lancement-facture");
?>  
